package util.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.lucene.queryParser.ParseException;

import util.ir.LMSearcher;
import util.ranker.TagRanking;

public class RankServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static String bigram_index = "/home/sergio/delicious_index/bigrams_index_filter2";
	public static String trigram_index = "/home/sergio/delicious_index/trigrams_index_filter2";

	float lambda = 0.9f;
	
	static LMSearcher trigram_searcher=null;
	static LMSearcher bigram_searcher=null; 
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RankServlet() {
		
		super();
		
		
		try {
			trigram_searcher = new LMSearcher(trigram_index);
			bigram_searcher = new LMSearcher(bigram_index);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		TagRanking ranker = new TagRanking(trigram_searcher, bigram_searcher);

		String query = request.getParameter("query");
		String seed_tags = request.getParameter("seeds");
		String n = request.getParameter("n");
		
		System.out.println(query);
		System.out.println(n);
		System.out.println(seed_tags);
		System.out.println(request.getAttribute("query"));
		
		
		if (query == null || seed_tags==null ) {

			query = "kids";
			PrintWriter out = response.getWriter();
			out.println("error");
			return;


		}
		
		
		if(query.equals("") || seed_tags.equals("")){
			PrintWriter out = response.getWriter();
			out.println("error");
			return;
		}

		String terms[] = query.split("\t");
		Hashtable<String, Float> t_terms = new Hashtable<String, Float>();
		for (int i = 0; i < terms.length; i++) {
			t_terms.put(terms[i], 1f);

		}

		Hashtable<String, Float> seeds = new Hashtable<String, Float>();

		String seed_terms[] = seed_tags.split("\t");

		for (int i = 0; i < seed_terms.length; i++) {
			String item = seed_terms[i];

			String val[] = item.split(" ");

			seeds.put(val[0], Float.valueOf(val[1]));

		}

		try {

			ranker.init(t_terms, seeds);
			Iterator<Entry<String, Float>> tuples = ranker.rankSuggestions();

			/**
			 * build html respond
			 */
			int limit = Integer.valueOf(n);
			int j = 0;
			String respond = "";
			while (j <= limit && tuples.hasNext()) {

				Entry<String, Float> tt = tuples.next();
				respond = respond + tt.getKey() + "\t " + tt.getValue() + "\n";

			}
			respond = respond.trim();
			
			ranker.clean();
			ranker=null;
			PrintWriter out = response.getWriter();
			out.println(respond);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
